from typing import Dict, Any
import time
from 新版2.系统组件.消息队列 import RabbitMQTickPublisher

class MQStockTickSaver:

    def __init__(self):
        self.saver = RabbitMQTickPublisher()

    def save_tick(self,stock_code:str,tick_data: Dict[str, Any]) -> bool:
        self.saver.publish_tick(stock_code=stock_code,tick_data=tick_data)
        return True

stock_saver_mq = MQStockTickSaver()

# 使用示例
if __name__ == "__main__":
    # 创建保存器（每5条或每秒自动刷新）
    saver = MQStockTickSaver()

    # 模拟实时Tick数据
    ticks = [
        {"code": "600000", "price": 12.34, "volume": 100, "time": "09:30:00"},
        {"code": "600000", "price": 12.35, "volume": 200, "time": "09:30:01"},
        {"code": "600000", "price": 12.36, "volume": 150, "time": "09:30:02"},
        {"code": "600000", "price": 12.33, "volume": 300, "time": "09:30:03"},
        {"code": "600000", "price": 12.32, "volume": 250, "time": "09:30:04"},
        {"code": "600000", "price": 12.31, "volume": 180, "time": "09:30:05"},
        {"code": "000001", "price": 15.20, "volume": 100, "time": "09:30:00"},
        {"code": "000001", "price": 15.21, "volume": 200, "time": "09:30:01"},
    ]

    # 逐个保存
    for tick in ticks:
        saver.save_tick(tick)
        time.sleep(0.1)  # 模拟实时间隔